package com.tzpay.tzpay.prefs;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Description:
 * Created:      [16/3/16]
 * Author:       [Yang]
 **/
public class UserPrefs {


    private static final String USER_PREFS = "USER_PREFS";

    /** 公司序号 */
    private static final String KEY_ORG_NO = "KEY_ORG_NO";
    /** 公司名字 */
    private static final String KEY_ORG_NAME = "KEY_ORG_NAME";
    /** 店铺编号 */
    private static final String KEY_SHOP_NO = "KEY_SHOP_NO";
    /** 店铺名字 */
    private static final String KEY_SHOP_NAME = "KEY_SHOP_NAME";
    /** 店铺电话 */
    private static final String KEY_SHOP_TEL = "KEY_SHOP_TEL";
    /** 操作员ID */
    private static final String KEY_OPR_ID = "KEY_OPR_ID";

    private static final String KEY_MERCHANT_NO = "KEY_MERCHANT_NO";

    private static final String KEY_SUB_MERCHANT_NO = "KEY_SUB_MERCHANT_NO";

    private static final String KEY_MERCHANT_DATA_KEY = "KEY_MERCHANT_DATA_KEY";

    private static final String KEY_MERCHANT_TRADE_KEY = "KEY_MERCHANT_TRADE_KEY";


    private static volatile UserPrefs singleton;
    private final SharedPreferences prefs;

    private boolean isLogin = false;
    private String merchantNo;
    private String subMerchantNo;
    private String merchantDataKey;
    private String merchantTradeKey;


    public static UserPrefs get(Context context) {
        if (singleton == null) {
            synchronized (UserPrefs.class) {
                singleton = new UserPrefs(context);
            }
        }
        return singleton;
    }

    private UserPrefs(Context context){
        prefs = context.getApplicationContext().getSharedPreferences(USER_PREFS,
                Context.MODE_PRIVATE);
        merchantDataKey = prefs.getString(KEY_MERCHANT_DATA_KEY,"");
        isLogin = TextUtils.isEmpty(merchantDataKey);
        if(isLogin){
            merchantNo = prefs.getString(KEY_MERCHANT_NO,"");
            subMerchantNo = prefs.getString(KEY_SUB_MERCHANT_NO, "");
            merchantTradeKey = prefs.getString(KEY_MERCHANT_TRADE_KEY,"");
        }
    }

    public boolean isLogin(){
        return isLogin;
    }

    public void setMerchantNo(String merchantNo){
        if(!TextUtils.isEmpty(merchantNo)){
            prefs.edit().putString(KEY_MERCHANT_NO,merchantNo).apply();
        }
    }

    public void setMerchantDataKey(String dataKey){
        if(!TextUtils.isEmpty(dataKey)){
            prefs.edit().putString(KEY_MERCHANT_DATA_KEY,dataKey).apply();
        }
    }

    public void setMerchantTradeKey(String tradeKey){
        if(!TextUtils.isEmpty(tradeKey)){
            prefs.edit().putString(KEY_MERCHANT_TRADE_KEY, tradeKey).apply();
        }
    }

    public void setSubMerchantNo(String subMerchantNo){
        if(!TextUtils.isEmpty(subMerchantNo)){
            prefs.edit().putString(KEY_SUB_MERCHANT_NO, subMerchantNo).apply();
        }
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public String getSubMerchantNo() {
        return subMerchantNo;
    }

    public String getMerchantDataKey() {
        return merchantDataKey;
    }

    public String getMerchantTradeKey() {
        return merchantTradeKey;
    }
}
